package com.itheima.mapper;

import com.itheima.pojo.Student;
import com.itheima.pojo.StudentCount;
import com.itheima.pojo.StudentDegree;
import org.apache.ibatis.annotations.Insert;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.Update;

import java.util.List;

@Mapper
public interface StudentMapper {
    @Select("select * from student where clazz_id = #{clazzId} limit 1")
    Student selectOneByClazzId(Integer clazzId);

    List<Student> pageList(String name, Integer degree, Integer clazzId);

    @Insert("INSERT INTO student (name, no, gender, phone, id_card, is_college, address, degree, graduation_date, clazz_id, violation_count, violation_score, create_time, update_time) " +
            "VALUES (#{name}, #{no}, #{gender}, #{phone}, #{idCard}, #{isCollege}, #{address}, #{degree}, #{graduationDate}, #{clazzId}, #{violationCount}, #{violationScore}, #{createTime}, #{updateTime})")
    void insert(Student student);

    @Select("select * from student where id = #{id}")
    Student selectById(Integer id);

    void updateById(Student student);

    void deleteByIds(Integer[] ids);

    @Update("update student set violation_count = violation_count+1, violation_score = violation_score + #{score} where id = #{studentId}")
    void violation(Integer studentId, Integer score);

    @Select("select case degree " +
            "            when 1 then '初中' " +
            "            when 2 then '高中' " +
            "            when 3 then '大专' " +
            "            when 4 then '本科' " +
            "            when 5 then '硕士' " +
            "            when 6 then '博士' " +
            "       end name, " +
            "       count(*) value " +
            "from student " +
            "group by degree")
    List<StudentDegree> studentDegreeData();

    @Select("select c.name clazz_name, count(s.id) cnt from clazz c left join student s on c.id = s.clazz_id group by c.name")
    List<StudentCount> studentCountData();
}
